package test;
import java.io.File;  
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;  
import java.util.List;  
import java.util.Map;

import org.apache.ecs.xhtml.map;
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.HttpStatus;  
import org.apache.http.NameValuePair;  
import org.apache.http.StatusLine;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.entity.StringEntity;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.DefaultHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.protocol.HTTP;  
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;  

public class test1 {
	
	                                      
	private static   String savefile = "D:\\test1.txt";  

	
		public static void main(String[] args) throws IOException {
			//System.out.println(doGet("http://218.88.252.36:9026/wishFourFiveSixSignUp?bmh=187104101882"));
			String html= doGet("http://218.88.252.36:9026/wishFourFiveSixSignUp?bmh=187104101882");
            Document doc = Jsoup.parse(html);  

			Elements rows = doc.select("select[id=fifth1]").get(0).select("option");  
			String ff="";
			String fff="";
			File file  = new File(savefile);
			Writer writer = new BufferedWriter(  
			        new OutputStreamWriter(  
			                new FileOutputStream(file), "UTF-8"));  
			for (int i = 0; i < rows.size(); i++) {
				if(!fff.equals(rows.get(i).attr("value"))){
					fff=rows.get(i).attr("value");
		 			String fifth1url="http://218.88.252.36:9026/goGetFiveYearPlanNormalMajor?schoolId="+rows.get(i).attr("value");
		 			//[{pc: "1", xxdm: "57013", xxmc: "川北幼儿师范高等专科学校", zydh: "02", zymc: "小学教育"},…]
		 		    //System.out.println(doPost(fifth1url,new HashMap()));
		 		    //解析 json
		 			
		 		    List<Map> map=JSONHelper.getListMap(doPost(fifth1url,new HashMap()));
		 		    for (int j = 0; j < map.size(); j++) {
		 		    	try {
		 		    		if(j % 9== 0){
								Thread.sleep(1000);
		 		    		}
						} catch (InterruptedException e) {
							
							e.printStackTrace();
						}

						System.out.println(map.get(j).get("xxmc")+"---"+map.get(j).get("zymc"));
						ff+="+++"+map.get(j).get("xxmc")+"---"+map.get(j).get("zymc");
					}
		 		  
					//System.out.println(rows.get(i).attr("value"));
					//System.out.println(rows.get(i).text());
				}
			}
			 writer.write(ff);
	 		   writer.flush();
				writer.close();
			/*String fifth1url="goGetFiveYearPlanNormalMajor?schoolId="
			if (select_id == "four1" || select_id == "four2" || select_id == "four3") {
				post_url = "goGetNinePlusFiveMajor" + "?schoolId=" + values;
			} else if (select_id == "fifth1" || select_id == "fifth2"){post_url = "goGetFiveYearPlanNormalMajor" + "?schoolId=" + values;}
			else if( select_id == "fifth3" || select_id == "fifth4") {post_url = "goGetFiveYearPlanNotNormalMajor" + "?schoolId=" + values;
			} else if (select_id == "sixth1" || select_id == "sixth2"
					|| select_id == "sixth3" || select_id == "sixth4") {
				post_url = "goGetThreeYearPlanMajor" + "?schoolId=" + values;
			} else {
				post_url = "check";
			}				
			*/
			/*	File file  = new File(savefile);
			Writer writer = new BufferedWriter(  
			        new OutputStreamWriter(  
			                new FileOutputStream(file), "UTF-8"));  
			
			writer.write(doGet("http://218.88.252.36:9026/wishFourFiveSixSignUp?bmh=187104101882"));
			writer.flush();
			writer.close();*/
			 
		}
		
		/** 
	     * get请求 
	     * @return 
	     */  
	    public static String doGet(String url) {  
	        try {  
	            HttpClient client = new DefaultHttpClient();  
	            //发送get请求  
	            HttpGet request = new HttpGet(url);  
	            request.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");  
	            request.setHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36");  
	            request.setHeader("Content-Type","text/html;charset=UTF-8");  
	            request.setHeader("Connection","keep-alive");  
	            request.setHeader("host","218.88.252.36:9026");  
	            request.setHeader("Accept-Language","zh-CN");  
	            request.setHeader("Referer","http://218.88.252.36:9026/check");  
	            request.setHeader("Cookie","JSESSIONID=EF49074E3939FD1E4DFD4EE803F07750");  

	            HttpResponse response = client.execute(request);  
	            
	            /**请求发送成功，并得到响应**/  
	            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
	                /**读取服务器返回过来的json字符串数据**/  
	                String strResult = EntityUtils.toString(response.getEntity());  
	                  
	                return strResult;  
	            }  
	        }   
	        catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	          
	        return null;  
	    }  
	      
	    /** 
	     * post请求(用于key-value格式的参数) 
	     * @param url 
	     * @param params 
	     * @return 
	     */  
	    public static String doPost(String url, Map params){  
	          
	        BufferedReader in = null;    
	        try {    
	            // 定义HttpClient    
	            HttpClient client = new DefaultHttpClient();    
	            // 实例化HTTP方法    
	            HttpPost request = new HttpPost(); 
	            request.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");  
	            request.setHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36");  
	            request.setHeader("Content-Type","text/html;charset=UTF-8");  
	            request.setHeader("Connection","keep-alive");  
	            request.setHeader("host","218.88.252.36:9026");  
	            request.setHeader("Accept-Language","zh-CN");  
	            request.setHeader("Referer","http://218.88.252.36:9026/check");  
	            request.setHeader("Cookie","JSESSIONID=EF49074E3939FD1E4DFD4EE803F07750");  

	            request.setURI(new URI(url));  
	              
	            //设置参数  
	            List<NameValuePair> nvps = new ArrayList<NameValuePair>();   
	            for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {  
	                String name = (String) iter.next();  
	                String value = String.valueOf(params.get(name));  
	                nvps.add(new BasicNameValuePair(name, value));  
	                  
	                //System.out.println(name +"-"+value);  
	            }  
	            request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));  
	              
	            HttpResponse response = client.execute(request);    
	            int code = response.getStatusLine().getStatusCode();  
	            if(code == 200){    //请求成功  
	                in = new BufferedReader(new InputStreamReader(response.getEntity()    
	                        .getContent(),"utf-8"));  
	                StringBuffer sb = new StringBuffer("");    
	                String line = "";    
	                String NL = System.getProperty("line.separator");    
	                while ((line = in.readLine()) != null) {    
	                    sb.append(line + NL);    
	                }  
	                  
	                in.close();    
	                  
	                return sb.toString();  
	            }  
	            else{   //  
	                System.out.println("状态码：" + code);  
	                return null;  
	            }  
	        }  
	        catch(Exception e){  
	            e.printStackTrace();  
	              
	            return null;  
	        }  
	    }  
	      
	    /** 
	     * post请求（用于请求json格式的参数） 
	     * @param url 
	     * @param params 
	     * @return 
	     */  
	    public static String doPost(String url, String params) throws Exception {  
	          
	        CloseableHttpClient httpclient = HttpClients.createDefault();  
	        HttpPost httpPost = new HttpPost(url);// 创建httpPost     
	        httpPost.setHeader("Accept", "application/json");   
	        httpPost.setHeader("Content-Type", "application/json");  
	        String charSet = "UTF-8";  
	        StringEntity entity = new StringEntity(params, charSet);  
	        httpPost.setEntity(entity);          
	        CloseableHttpResponse response = null;  
	          
	        try {  
	              
	            response = httpclient.execute(httpPost);  
	            StatusLine status = response.getStatusLine();  
	            int state = status.getStatusCode();  
	            if (state == HttpStatus.SC_OK) {  
	                HttpEntity responseEntity = response.getEntity();  
	                String jsonString = EntityUtils.toString(responseEntity);  
	                return jsonString;  
	            }  
	            else{  
	                System.out.println("请求返回:"+state+"("+url+")");  
	            }  
	        }  
	        finally {  
	            if (response != null) {  
	                try {  
	                    response.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	            try {  
	                httpclient.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return null;  
	    }  
}
